/*******************************************************************************
 *  Copyright (c) 2010 Chase Technology Ltd.
 *  
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the GNU General Public License v3.0
 *  which is available at http://www.gnu.org/licenses/
 * 
 * Contributors:
 *     Doug Satchwell - initial API and implementation
 *******************************************************************************/
package org.jfunction;

import java.lang.reflect.Method;
import java.util.Map;

import org.jfunction.annotations.Step;

public class ExecutableStep {
	private final Method method;
	private final Map<String,String> varMap;
	
	public ExecutableStep(Method method, Map<String, String> varMap) {
		super();
		this.method = method;
		this.varMap = varMap;
	}
	
	public Method getMethod() {
		return method;
	}
	
	public String getText() {
		return method.getAnnotation(Step.class).value();
	}
	
	public Object[] getArgs(Example example) {
		Object[] args = new Object[varMap.size()];
		int j = 0;
		for (String key : varMap.keySet()) {
			Object value = getValue(example,key);
			args[j++] = value;
		}
		return args;
	}
	
	public boolean hasValue(Example example, String key) {
		return example.hasValue(varMap.get(key));
	}

	public Object getValue(Example example, String key) {
		return example.getValue(varMap.get(key));
	}

	public void setValue(Example example, String key, Object value) {
		example.setValue(varMap.get(key),value);
	}
}